{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 单层线性感知机实现\n",
    "- 使用的是批处理方式，先算出每一个wi总的改变量，最后更新w。迭代更新w，当相隔10位的准确率的差小于0.01或者迭代次数达到要求时，结束迭代。计算结果大于等于0.5，输出是1；小于0.5，输出是0。两类是可以使用一根直线隔开的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import accuracy_score\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#学习权重函数\n",
    "def one_perceptron(input_x,input_y,C,test_x=None,max_round=60): \n",
    "    '''\n",
    "    C是学习率,max_round是迭代次数\n",
    "    当准确率的隔10位改变量小于0.01时，或者迭代次数大于max_round时，退出循环，输出w和准确率\n",
    "    当测试集不是None时，输出预测值\n",
    "    \n",
    "    '''\n",
    "    assert len(input_x)==len(input_y)\n",
    "    w=np.random.rand(1,3).ravel()#初始化权重w,展平数据\n",
    "    input_x=np.c_[input_x,np.ones(len(input_x))]#增加x0=1\n",
    "    pred_y=np.dot(w,input_x.T)#计算预测值\n",
    "    max_r=0\n",
    "    acc_change=1\n",
    "    acc=[]\n",
    "    while (abs(acc_change)>0.01)&(max_r<max_round):\n",
    "        input_y_pred=C*(input_y-pred_y)#差值乘以学习率\n",
    "        \n",
    "        #计算权重应该修改的量\n",
    "        C_y_pred=input_y_pred.reshape(-1,1)\n",
    "        change_w=(input_x*C_y_pred).sum(axis=0)\n",
    "        w=w+change_w\n",
    "        \n",
    "        #迭代的准确率\n",
    "        pred_y=np.dot(w,input_x.T)\n",
    "        predict_y_int=[1 if i>=0.5 else 0 for i in pred_y ]#大于等于0.5等于1，小于0.5等于0\n",
    "        acc.append(accuracy_score(input_y,predict_y_int))\n",
    "        if max_r>10:\n",
    "            acc_change=acc[max_r]-acc[max_r-10]#\n",
    "        max_r+=1\n",
    "    print('权重w的值：',w)\n",
    "    print('准确率：{}%'.format(100*acc[max_r-1]))\n",
    "    if test_x is not None:\n",
    "        test_x=np.c_[test_x,np.ones(len(test_x))]#增加x0=1\n",
    "        pred_test_y=np.dot(w,test_x.T)\n",
    "        \n",
    "        pred_test_y=[1 if i>=0.5 else 0 for i in pred_test_y]#大于等于0.5等于1，小于0.5等于0\n",
    "        print('预测值：',pred_test_y)\n",
    "        return w,acc[max_r-1],pred_test_y\n",
    "    return w,acc[max_r-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置的输入数据和测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "input_x=np.array([[0,2],[-1,-2],[0,0],[1,0],[0,1],[6,1],[6,3],[1,6],[1,-6],[-1,-1]])\n",
    "y=np.array([0,0,0,0,0,1,1,1,0,0])\n",
    "test=np.array([[0,-1],[0,0.6],[1,10],[-6,1],[1,-1],[6,1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 训练集不同类是可以使用一根直线隔开的，属于单层感知机线性可分问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'Y_input')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEXCAYAAACqIS9uAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYi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OJpugQyQt5O2mRSLa5e7OZzQHuJbrIq1tmNje+UAkzO59orpd9geFEs31+B/g8MARY\nBRwLnBi3c118hD+ZaP6dbwAj4mVfdfdV2X1nIv2jI34JzcPAFkB/J24b5+6TgNuB/eNlj7v7fsAK\noMsZFd39MuBM4Lr4qleFvqROwS+hOQv4M9ERe3/8Lv79FtB+Y5eF8e/niLpzMg2G+eolUAp+CYaZ\nVQE7Ec3YeYqZ9Seca7tYNiH+vTvRJFtNRPP/QzQNR7t6oi6h9imYRVKl4JeQnANc6u71RPdCOG0j\nX2/PeKbKEUS3T3wEONzMfk004Vq7RcD4+LaV+TxhnARCUzaIDEB8wnduTyODRPKVgl+CFR+tZ/rQ\n3XN+2zuRtCn4RUQCoz5+EZHAKPhFRAKj4BcRCYyCX0QkMAp+EZHA/D+C8pKlVvx5qgAAAABJRU5E\nrkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x21f60d71eb8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig=plt.figure()\n",
    "ax=fig.add_subplot(111)\n",
    "ax.scatter(input_x[:,0],input_x[:,1],c=y)\n",
    "ax.set_title('训练集分布',size=16)\n",
    "ax.set_xlabel('X_input')\n",
    "ax.set_ylabel('Y_input')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过不断的迭代训练，w值基本不怎么改变了，可以看到预测结果基本正确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "权重w的值： [ 0.14284657  0.06550683  0.02693114]\n",
      "准确率：100.0%\n",
      "预测值： [0, 0, 1, 0, 0, 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([ 0.14284657,  0.06550683,  0.02693114]), 1.0, [0, 0, 1, 0, 0, 1])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_perceptron(input_x,y,0.01,test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
